package org.itboys.mobile.controller.mobile;

import com.google.common.collect.Maps;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;

import org.itboys.kitchen.util.MobileSignUtils;
import org.itboys.mobile.service.mongo.mobile.SundayMobileCodeService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年2月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_验证码_controller。
 */
@RestController
@RequestMapping("/sunday/mobile/code")
public class SundayMobileCodeController extends BaseController {
    @Autowired
    private SundayMobileCodeService codeService;



    /**
     * 验证码发送
     * @param request
     * @param response
     * @param mobile
     * @param type
     */
    @RequestMapping(value = "/send", method = RequestMethod.POST)
    @ApiOperation(value = "发送验证码。code!=0直接提示消息")
    public void send(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "mobile", value = "手机号") @RequestParam(value = "mobile", required = true) String mobile,
            @ApiParam(required = true, name = "type", value = "验证码类型。1登陆（手机号加验证码登录），2注册" +
                    "，3修改密码，4忘记密码，5设置支付密码，6修改支付密码") @RequestParam(value = "type", required = true) Integer type,
            @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){
        try{
            //2017年6月20日校验签名
            Map<String, Object> param = Maps.newHashMapWithExpectedSize(4);
            param.put("mobile",mobile);
            param.put("type",type);
            param.put("nonce", nonce);
            param.put("sign", sign);
           /* boolean signSer = MobileSignUtils.signCheck(param, sign);
            if(!signSer){
                logger.warn("签名校验失败");
                ResponseMessage.error(-1,"签名校验失败");
                return;
            }*/
            int resultCode= codeService.sendCode(mobile,type);
            if(resultCode==-2){
                ResponseMessage.error(-2,"请输入正确的手机号。");
            }else{
                ResponseMessage.success();
            }
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/code/send----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    /**
     * 验证码发送
     * @param request
     * @param response
     * @param mobile
     * @param type
     */
    @RequestMapping(value = "/send2", method = RequestMethod.POST)
    @ApiOperation(value = "发送验证码。code!=0直接提示消息")
    public void send2(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "mobile", value = "手机号") @RequestParam(value = "mobile", required = true) String mobile,
            @ApiParam(required = true, name = "type", value = "验证码类型。1登陆（手机号加验证码登录），2注册" +
                    "，3修改密码，4忘记密码，5设置支付密码，6修改支付密码") @RequestParam(value = "type", required = true) Integer type
            ){
        try{

            int resultCode= codeService.sendCode(mobile,type);
            if(resultCode==-2){
                ResponseMessage.error(-2,"请输入正确的手机号。");
            }else{
                ResponseMessage.success();
            }
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/code/send----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    /**
     * 验证码发送
     * @param request
     * @param response
     * @param mobile
     * @param type
     */
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    @ApiOperation(value = "检验验证码。code!=0直接提示消息")
    public void getCart(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "mobile", value = "手机号") @RequestParam(value = "mobile", required = true) String mobile,
            @ApiParam(required = true, name = "type", value = "验证码类型。1登陆（手机号加验证码登录），2注册" +
                    "，3修改密码，4忘记密码，5设置支付密码，6修改支付密码") @RequestParam(value = "type", required = true) Integer type,
            @ApiParam(required = true, name = "code", value = "验证码")@RequestParam(value = "code", required = true) String code){
        try{
            int resultCode= codeService.checkCode(mobile,code,type);
            if(resultCode==-1){
                ResponseMessage.error(-1,"系统错误。");
            }else if(resultCode==-2) {
                ResponseMessage.error(-2, "请输入正确的手机号。");
            }else if(resultCode==-3){
                ResponseMessage.error(-3,"验证码错误。");
            }else{
                ResponseMessage.success();
            }
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code2=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code2=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/code/check----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code2,msg);
        }
    }

}
